# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  include TagsHelper
  
  def show_main_nav
    home = "#{link_to_unless_current 'Home', :controller => :welcome, :action => :index}"
    new_article = "#{link_to_unless_current 'New Article', :controller => :articles, :action => :new}"
    new_category = "#{link_to_unless_current 'New Category', :controller => :categories, :action => :new}"
    admin = "#{link_to_unless_current 'Admin', :controller => :welcome, :action => :admin}"    
    recipe = "#{link_to_unless_current 'Recipes', :controller => :recipes, :action => :index}"
    if admin?
      content_tag(:ul, 
        content_tag(:li, home) + 
        content_tag(:li, new_article) + 
        content_tag(:li, new_category) + 
        content_tag(:li, admin) +
        content_tag(:li, recipe)        
      )  
    else
      content_tag(:ul, 
        content_tag(:li, home)
      )            
    end
  end
  
  def show_footer
    content_tag(:p, "#{COPYRIGHT}") + content_tag(:p, "Version:#{VERSION}")
  end
  
  def show_tag_cloud(tag_cloud = "")
    tags = Tag.counts
    title = content_tag(:h2, "Tags")
    tag_cloud tags, %w(css1 css2 css3 css4 css5 css6) do |tag, css_class|
      tag_cloud << "#{link_to tag, { :controller => :welcome, :action => :list, :tag => tag.name }, :class => css_class + ' tag'} "
    end
    title + content_tag(:div, tag_cloud, :class => "tag_warp")
  end
  
  def show_Languages(languages = "")
    return if controller.action_name == "admin"
    title = content_tag(:h2, "Languages")
    Article::LANGUAGES.each do |display, value|
      link_tag = link_to(display, :controller => :welcome, :action => :list, :language => value)
      count_tag = content_tag(:span, "(#{Article.count_by_language(value)})")
      languages << content_tag(:li, "#{link_tag} #{count_tag}")
    end
    title + content_tag(:ul, languages)
  end
  
  def show_categories(categories = "")
    if controller.action_name == "admin"
      title = content_tag(:h2, ("Categories" + content_tag(:span, "(Edit Mode)")))
    else
      title = content_tag(:h2, "Categories")      
    end 
    Category.all.each do |category|
      link_tag = link_to(category.name, :controller => :welcome, :action => :list, :category => category)
      count_tag = content_tag(:span, "(#{category.articles.count})")
      edit_tag = link_to(image_tag("/images/icons/pencil.png"), :controller => :categories, :action => :edit, :id => category)
      category_content = controller.action_name == "admin" ? content_tag(:li, "#{link_tag} #{count_tag} #{edit_tag}") : content_tag(:li, "#{link_tag} #{count_tag}")
      categories << category_content
    end
    title + content_tag(:ul, categories)
  end
  
  def show_recipes(recipes = "")
    return if controller.action_name != "admin"
    title = content_tag(:h2, ("Recipes" + content_tag(:span, "(Edit Mode)")))
    contents = ""
    Recipe::GROUPS.each do |k, v|
      contents << content_tag(:h3, k)
      recipes_content = ""
      Recipe.find(:all, :conditions => {:group => v}).each do |recipe|
        count_tag = content_tag(:span, "(#{recipe.articles.count})")
        edit_tag = link_to(image_tag("/images/icons/pencil.png"), :controller => :recipes, :action => :edit, :id => recipe)
        recipes_content << content_tag(:li, "#{recipe.name} #{count_tag} #{edit_tag}")
      end      
      contents << content_tag(:ul, recipes_content)
    end
    title + contents + content_tag(:p, link_to("New Recipe", :controller => :recipes, :action => :new), :class => "side_new_btn")
  end
  
  def admin?
    request.remote_ip == "127.0.0.1"
  end
  
  def show_tag_name_warp_by_span(article, tag)
    class_name = article.tag_list.include?(tag.name) ? "selected" : ""
    content_tag(:span, tag.name, :class => class_name)
  end
end
